import java.util.*;

public class ObserverPattern {
    public static void main(String[] args) {
        ConcreteSubject subject=new ConcreteSubject();
        subject.attach(new ConcreteObserver(subject));
        subject.action="哈哈哈";
        subject.notifyObserver();
        subject.action="可以啊，小伙子";
        subject.notifyObserver();

    }
}

interface Subject {
    void attach(Observer observer);

    void detach(Observer observer);

    void notifyObserver();
}

class ConcreteSubject implements Subject {
    List<Observer> observers;
    String action;
    public ConcreteSubject(){
        observers=new ArrayList<>();
        action="";
    }
    @Override
    public void attach(Observer observer) {
       observers.add(observer);
    }

    @Override
    public void detach(Observer observer) {
       observers.remove(observer);
    }

    @Override
    public void notifyObserver() {
        for (Observer observer : observers) {
            observer.update(); 
        }
    }

}

interface Observer {

    void update();
}

class ConcreteObserver implements Observer {
    public String action;
    public ConcreteSubject subject;
    public ConcreteObserver(ConcreteSubject subject){
        this.subject=subject;
    }

    @Override
    public void update() {
        action=subject.action;
        System.out.println(action);
    }



}